package com.cai.quartz.schedule;

import com.cai.quartz.trigger.MySimpleTrigger;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
import java.util.Properties;

//@Configuration
public class MySimpleSchedule {
    public MySimpleSchedule() throws SchedulerException, InterruptedException {
        doSomeThing();
    }

    public void doSomeThing() throws SchedulerException, InterruptedException {
        //调度器
        StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
        //创建配置工厂的属性的对象
        Properties properties=new Properties();
        //定义线程
        properties.put(StdSchedulerFactory.PROP_THREAD_EXECUTOR_CLASS,"org.quartz.simpl.SimpleThreadPool");
        properties.put("org.quartz.threadPool.threadCount","-1");

        try {
            stdSchedulerFactory.initialize(properties);
            Scheduler scheduler = stdSchedulerFactory.getScheduler();
            scheduler.start();
        }catch (Exception e){
            e.printStackTrace();
        }

        //任务实例
        JobDetail jobDetail=JobBuilder
                .newJob(MySimpleTrigger.class)
                .withIdentity("job1")//定义唯一标识
                .build();

        Date startDate = DateBuilder.futureDate(3, DateBuilder.IntervalUnit.SECOND);
        Date endDate=DateBuilder.futureDate(5, DateBuilder.IntervalUnit.SECOND);
        //触发器
        Trigger trigger=TriggerBuilder
                .newTrigger()
                .withIdentity("trigger1")//定义唯一标识
                .startAt(startDate)
                //.withSchedule(SimpleScheduleBuilder
                //        .repeatSecondlyForever(5)
                //        .withRepeatCount(3))

                .withSchedule(CronScheduleBuilder
                        .cronSchedule("* * * * * ?"))
                //.endAt(endDate)
                .build();

        //Date date = scheduler.scheduleJob(jobDetail, trigger);
        //scheduler.start();
        //
        //Thread.sleep(5000L);
        //scheduler.shutdown(true);
    }
}
